package com.morequick.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;



@Controller
@RequestMapping("/admin")
public class UploadControler {

	@RequestMapping("/upload")
	public String upload(@RequestParam("file") MultipartFile file) throws Exception{


		System.out.println(file);
		/*if(file !=null ){
			//文件保存目录路径
			String savePath = request.getServletContext().getRealPath("/") + "finder/";
	
			//文件保存目录URL
			String saveUrl  = request.getContextPath() + "/finder/";
			
			//String fileName =file.getOriginalFilename();
			String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			
			//定义允许上传的文件扩展名
			HashMap<String, String> extMap = new HashMap<String, String>();
			extMap.put("image", "gif,jpg,jpeg,png,bmp");
			
			HashMap<String, Object> result = new HashMap<String, Object>();
			
			if(!Arrays.<String>asList(extMap.get("image").split(",")).contains(fileExt)){
	
				result.put("error","只允许" + extMap.get("image") + "格式。");
				
				ResponseUtil.write(response, result);
				
				return null;
			}
			
			if(file.getSize()>1000000){
				
				result.put("error","文件过大");
				
				ResponseUtil.write(response, result);
				
				return null;
			}
			
			String dirName = request.getParameter("file");
			if (dirName == null) {
				dirName = "images";
			}
			
			//创建文件夹
			savePath += dirName + "/";
			saveUrl += dirName + "/";
			File saveDirFile = new File(savePath);
			if (!saveDirFile.exists()) {
				saveDirFile.mkdirs();
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());
			savePath += ymd + "/";
			saveUrl += ymd + "/";
			File dirFile = new File(savePath);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
	
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
			try{
				File uploadedFile = new File(savePath, newFileName);
				file.transferTo(uploadedFile);
			}catch(Exception e){
				e.getMessage();
			}
		}*/
		
		return null;
	}
	
	
	
}
